/**
 *
 */
package com.cpsdna.openapi.client;

import com.cpsdna.openapi.bean.req.Auth;
import com.cpsdna.openapi.bean.req.CmdRequest;
import com.cpsdna.openapi.bean.resp.CmdReturnResult;
import com.cpsdna.openapi.client.bean.SearchVehicleCmdReturnResult;
import com.cpsdna.openapi.client.bean.SearchVehicleParams;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wud
 */
public class Demo {
	private static final Logger log = LoggerFactory.getLogger(Demo.class);


	/*
	* 使用之前需要首先修改Consts类中的配置。
	* */
	private Gson buildGson() {
		Gson gson = new GsonBuilder()
				.enableComplexMapKeySerialization()
				.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		return gson;
	}

	/**
	 * 调用openapi请求
	 */
	private <T extends CmdReturnResult> T invokeOpenAPI(CmdRequest cmd, Class<T> cmdReturnResultClass) {
		// 使用apache httpclient 4.5.2
		Gson gson = this.buildGson();

		String content = null; // 响应内容
		String pm = null; // 请求内容
		// ---------------------------------------

		pm = gson.toJson(cmd).toString();

		log.debug("post json=" + pm);
		// ---------------------------------------

		EntityBuilder eb = EntityBuilder.create();
		eb.setContentEncoding("utf-8");
		eb.setContentType(ContentType.APPLICATION_JSON);
		eb.setText(pm);
		// ---------------------------------------
		try {
			CloseableHttpClient httpClient = HttpClients.custom().build();
			HttpPost httpPost = new HttpPost(Consts.url);
			try {
				httpPost.setEntity(eb.build());
				HttpResponse response = httpClient.execute(httpPost);
				HttpEntity entity = response.getEntity();

				if (entity != null) {
					content = EntityUtils.toString(entity, "UTF-8");
				} else {
					content = "";
				}
				log.debug("open api returns:" + content);
				T r = gson.fromJson(content, cmdReturnResultClass);
				return r;
			} finally {
				httpClient.close();
			}
		} catch (Exception e) {
			log.error("post openapi error ", e);
			return null;

		}
	}

	private Auth buildAuth() {
		Auth auth = new Auth();
		auth.setDevKey(Consts.devKey);
		auth.setPassword(Consts.password);
		auth.setSecret(Consts.secret);
		auth.setUserName(Consts.userName);
		return auth;
	}

	public static void main(String args[]) {
		Demo d = new Demo();

		//登录
		Auth auth = d.buildAuth();
		CmdRequest<SearchVehicleParams> cmd = new CmdRequest<>();
		cmd.setCmd("searchVehicle");
		cmd.setAuth(auth);
		cmd.setParams(new SearchVehicleParams());
		//"苏A12345" 是一个不存在的车 仅供参考用
		cmd.getParams().setLpno("苏A12345");

		// 请求数据

		CmdReturnResult r = d.invokeOpenAPI(cmd, SearchVehicleCmdReturnResult.class);
		if (r != null) {
			log.info(r.toString());
		}
	}
}
